<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=0.4"/>
<meta name="google" content="notranslate" />
<link rel="canonical" href="https://sleef.org/compile.xhtml" />
<link rel="icon" href="favicon.png" />
<link rel="stylesheet" type="text/css" href="texlike.css"/>
<link rel="stylesheet" type="text/css" href="sleef.css"/>
<title>SLEEF - Compiling and installing the library</title>
</head>
<body translate="no" class="notranslate">
<h1>SLEEF Documentation - Compiling and installing the library</h1>

<h2>Table of contents</h2>

<ul class="none" style="font-family: arial, sansserif; padding-left: 0.5cm;">
  <li><a class="underlined" href="index.xhtml">Introduction</a></li>
  <li>&nbsp;</li>
  <li><a class="underlined" href="compile.xhtml">Compiling and installing the library</a></li>
    <ul class="disc">
      <li><a href="compile.xhtml#preliminaries">Preliminaries</a></li>
      <li><a href="compile.xhtml#cmakevars">Common CMake variables</a></li>
      <li><a href="compile.xhtml#linux">Compiling and installing the library on Linux</a></li>
      <li><a href="compile.xhtml#debian">Installing the library on Debian and Ubuntu</a></li>
      <li><a href="compile.xhtml#MSVC">Compiling the library with Microsoft Visual C++</a></li>
      <li><a href="compile.xhtml#hello">Compiling and running "Hello SLEEF"</a></li>
      <li><a href="compile.xhtml#import">Importing SLEEF into your project</a></li>
    </ul>
  <li>&nbsp;</li>
  <li><a class="underlined" href="purec.xhtml">Math library reference</a></li>
  <li><a class="underlined" href="dft.xhtml">DFT library reference</a></li>
  <li><a class="underlined" href="misc.xhtml">Other tools included in the package</a></li>
  <li><a class="underlined" href="benchmark.xhtml">Benchmark results</a></li>
  <li><a class="underlined" href="additional.xhtml">Additional notes</a></li>
</ul>

<h2 id="preliminaries">Preliminaries</h2>

<p class="noindent">
In order to build SLEEF, you need <a class="underlined"
href="http://www.cmake.org/">CMake</a>, which is an open-source and
cross-platform building tool. In order to test the library, it is
better to have <a class="underlined" href="http://www.mpfr.org/">the
GNU MPFR Library</a>, <a class="underlined"
href="https://wiki.openssl.org/index.php/Libssl_API">Libssl</a> and
<a class="underlined" href="http://www.fftw.org/">FFTW</a>.
</p>

<p>
CMake works by allowing the developer to specify build parameters and
rules in a simple text file that cmake then processes to generate
project files for the actual native build tools (e.g. UNIX Makefiles,
Microsoft Visual Studio, Apple XCode, etc). If you are not already
familiar with cmake, please refer to the
<a class="underlined" href="https://cmake.org/documentation/">official documentation</a>
or
the <a class="underlined" href="https://gitlab.kitware.com/cmake/community/-/wikis/home">basic
introductions in the wiki</a>.
</p>

<h3>Quick start</h3>

<p class="noindent">
1. Make sure cmake is available on the command-line. The command below
should display a version number greater than or equal to 3.5.1.
</p>
<pre class="command">$ cmake --version</pre>

<p class="noindent">
2. Checkout out the source code from our <a class="underlined"
href="https://github.com/shibatch/sleef">GitHub repository</a>.
</p>
<pre class="command">$ git clone https://github.com/shibatch/sleef</pre>

<p class="noindent">
3. Make a separate directory to create an out-of-source build. SLEEF does not
allow for in-tree builds.
</p>
<pre class="command">$ cd sleef
$ mkdir build &amp;&amp; cd build</pre>

<p class="noindent">
4. Run cmake to configure your project and generate the system to build it:
</p>
<pre class="command">$ cmake ..</pre>

<p class="noindent">
See the list of <a class="underlined" href="#cmakevars">options and
variables</a> for customizing your build.
</p>

<p class="noindent" style="margin-top: 1em;">
5. Now that you have the build files created by cmake, proceed from the top
of the build directory:
</p>
<pre class="command">$ make</pre>

<p class="noindent">
6. You can execute the tests by running:
</p>
<pre class="command">$ make test</pre>

<p class="noindent">
7. Install the library under ../my-sleef/install by running:
</p>
<pre class="command">$ make install</pre>


<h4 id="cmakevars">Common CMake variables</h4>

<p>
Below is the list of common cmake variables that are used to
configure a build for SLEEF.
</p>

<ul style="margin-top: 1em; margin-bottom: 1em;">
  <li><b>CMAKE_BUILD_TYPE</b>: By default, CMake supports the following configuration:</li>
  <ul>
    <li>`Release`: Basic optimizations are turned on. This is the default setting.</li>
    <li>`Debug`: Basic debug flags are turned on. Optimization is disabled.</li>
    <li>`MinSizeRel`: Builds the smallest (but not fastest) object code</li>
    <li>`RelWithDebInfo`: Builds optimized code with debug information as well</li>
  </ul>
  <li><b>BUILD_SHARED_LIBS</b> : Static libs are built if set to
  FALSE</li>
  <li><b>CMAKE_C_FLAGS_RELEASE</b> : The optimization options used by
  the compiler.</li>
  <li><b>CMAKE_INSTALL_PREFIX</b> : The prefix it uses when running
  `make install`. Defaults to /usr/local on GNU/Linux and
  MacOS. Defaults to C:/Program Files on Windows.</li>
</ul>

<h4>SLEEF-specific CMake variables</h4>

<p>
Below is the list of SLEEF-specific cmake variables.
</p>

<ul style="margin-top: 1em; margin-bottom: 1em;">
  <!-- Options for building the entire library -->
  <li><b>SLEEF_SHOW_CONFIG</b> : Show relevant
  cmake variables upon configuring a build</li>
  <li><b>SLEEF_SHOW_ERROR_LOG</b> : Show the content of
  CMakeError.log</li>
  <li><b>BUILD_TESTS</b> : Avoid building testing
  tools if set to FALSE</li>
  <li><b>ENABLE_ALTDIV</b> : Enable alternative division method (aarch64 only)</li>
  <li><b>ENABLE_ALTSQRT</b> : Enable alternative sqrt method (aarch64 only)</li>
  <li><b>DISABLE_LONG_DOUBLE</b> : Disable support for long double data type</li>
  <li><b>ENFORCE_LONG_DOUBLE</b> : Build fails if long double data type is
  not supported by the compiler</li>
  <li><b>DISABLE_FLOAT128</b> : Disable support for float128 data type</li>
  <li><b>ENFORCE_FLOAT128</b> : Build fails if float128 data type is not
  supported by the compiler</li>
  <li><b>DISABLE_OPENMP</b> : Disable support for OpenMP</li>
  <li><b>ENFORCE_OPENMP</b> : Build fails if OpenMP is not
  supported by the compiler</li>

  <!-- Options for LTO -->
  <li><b>ENABLE_LTO</b> : Enable support for LTO with gcc, or thinLTO
  with llvm</li>
  <li><b>LLVM_AR_COMMAND</b> : Specify LLVM AR command when you build
  the library with thinLTO support with clang.</li>
  <li><b>SLEEF_ENABLE_LLVM_BITCODE</b> : Generate LLVM bitcode</li>
  <li><b>BUILD_INLINE_HEADERS</b> : Generate header files for inlining
  whole SLEEF functions</li>

  <!-- Options regarding to vector extensions -->
  <li style="margin-top: 1em;"><b>DISABLE_SSE2</b> : Disable support for x86 SSE2</li>
  <li><b>ENFORCE_SSE2</b> : Build fails if SSE2 is not
  supported by the compiler</li>
  <li><b>DISABLE_SSE4</b> : Disable support for x86 SSE4</li>
  <li><b>ENFORCE_SSE4</b> : Build fails if SSE4 is not
  supported by the compiler</li>
  <li><b>DISABLE_AVX</b> : Disable support for x86 AVX</li>
  <li><b>ENFORCE_AVX</b> : Build fails if AVX is not
  supported by the compiler</li>
  <li><b>DISABLE_FMA4</b> : Disable support for x86 FMA4</li>
  <li><b>ENFORCE_FMA4</b> : Build fails if FMA4 is not
  supported by the compiler</li>
  <li><b>DISABLE_AVX2</b> : Disable support for x86 AVX2</li>
  <li><b>ENFORCE_AVX2</b> : Build fails if AVX2 is not
  supported by the compiler</li>
  <li><b>DISABLE_AVX512F</b> : Disable support for x86 AVX512F</li>
  <li><b>ENFORCE_AVX512F</b> : Build fails if AVX512F is not
  supported by the compiler</li>
  <li><b>DISABLE_SVE</b> : Disable support for AArch64 SVE</li>
  <li><b>ENFORCE_SVE</b> : Build fails if SVE is not
  supported by the compiler</li>
  <li><b>DISABLE_VSX</b> : Disable support for PowerPC VSX</li>
  <li><b>ENFORCE_VSX</b> : Build fails if VSX is not
  supported by the compiler</li>
  <li><b>DISABLE_ZVECTOR2</b> : Disable support for ZVECTOR2</li>
  <li><b>ENFORCE_ZVECTOR2</b> : Build fails if ZVECTOR2 is not
  supported by the compiler</li>

  <!-- Options for libm -->
  <li style="margin-top: 1em;"><b>BUILD_GNUABI_LIBS</b> : Avoid building
  libraries with GNU ABI if set to FALSE</li>
  <li><b>ENFORCE_TESTER3</b> : Build fails if
  tester3 cannot be built</li>

  <!-- Options for DFT -->
  <li style="margin-top: 1em;"><b>BUILD_DFT</b> : Avoid building DFT
  libraries if set to FALSE</li>
  <li><b>SLEEFDFT_MAXBUTWIDTH</b> : This variable
  specifies the maximum length of combined butterfly block used in the
  DFT. Setting this value to 7 makes DFT faster but compilation takes
  more time and the library size will be larger.</li>
  <li><b>DISABLE_FFTW</b> : Disable FFW-based testing of the DFT
  library.</li>

  <!-- Options for Quad -->
  <li style="margin-top: 1em;"><b>BUILD_QUAD</b> : An <b>experimental</b> quad-precision
  library will be built if set to TRUE</li>
</ul>

<h2 id="linux">Compiling and installing the library on Linux</h2>

<p class="noindent">
  In order to build the library, you need to install OpenMP. In order
  to test the library, you need to install libmpfr, libssl and
  libfftw3. Availability of these libraries are checked upon execution
  of cmake. Please change the directory to sleef-3.X and run the
  following commands.

  <pre class="command">$ sudo apt-get install libmpfr-dev libssl-dev libfftw3-dev
$ mkdir build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=/usr ..
$ make
$ make test
$ sudo make install</pre>
</p>

<p>
  <b style="color:red">Parallel build is only supported with Ninja.</b>
</p>

<br/>

<p>
  In order to uninstall the libraries and headers, run the following command.
</p>

<pre class="command" style="margin-top: 1em;">$ sudo xargs rm -v &lt; install_manifest.txt</pre>

<h3 id="lto">Building the library with LTO support</h3>

<p>
  You can build the library with link time opimization(LTO) support
  with the following commands. Note that you can only build static
  libraries with LTO support. You also have to use the same compiler
  with the same version to build the library and other source codes.
</p>

<pre class="command">$ CC=gcc cmake -DBUILD_SHARED_LIBS=FALSE -DENABLE_LTO=TRUE ..</pre>

<p>
  In order to build the library with thinLTO support with clang, you
  need to specify LLVM AR command that exactly corresponds to the
  clang compiler.
</p>

<pre class="command">$ CC=clang-9 cmake -DBUILD_SHARED_LIBS=FALSE -DENABLE_LTO=TRUE -DLLVM_AR_COMMAND=llvm-ar-9 ..</pre>


<h3 id="inline">Building the header files for inlining the whole SLEEF functions</h3>

<p>
  Header files for inlining the whole SLEEF functions can be built
  with the following commands. With these header files, it may be
  easier to inline the whole SLEEF functions than using LTO. You have
  to specify "-ffp-contract=off" compiler option when compiling a
  source code that includes one of these header files.
</p>

<pre class="command">$ cmake -DBUILD_INLINE_HEADERS=TRUE ..</pre>

<h2 id="debian">Installing the library on Debian and Ubuntu</h2>

<p class="noindent">
  If you are using Debian 10(Buster), Ubuntu 18.04(Bionic) or later,
  then you can install the library through apt-get. The DFT library
  will not be installed.

  <pre class="command">$ sudo apt-get update
$ sudo apt-get install libsleef-dev</pre>
</p>

<h2 id="MSVC">Compiling the library with Microsoft Visual C++</h2>

<p class="noindent">
  You need Visual Studio 2019. Open developer command prompt for
  VS2019 and change directory to sleef-3.X. When configuring a build
  with cmake, you need to use a specific generator: `cmake -G"Visual
  Studio 16 2019" ..` This generator will create a proper solution
  `SLEEF.sln` under the build directory. You can still use `cmake
  --build .` to build the library without opening Visual Studio.
</p>

<p>
  Below is an example of commands for building SLEEF with Visual
  Studio.
</p>

<pre class="command">D:\sleef-3.X> mkdir build
D:\sleef-3.X> cd build
D:\sleef-3.X\build> cmake -G"Visual Studio 15 2017 Win64" ..    &amp;:: If you are using VS2017
D:\sleef-3.X\build> cmake -G"Visual Studio 16 2019" ..          &amp;:: If you are using VS2019
D:\sleef-3.X\build> cmake --build . --config Release -- /maxcpucount:1</pre>


<h3 id="cow">Compiling the library with Clang on Windows</h3>

<p class="noindent">
  You need Visual Studio 2019. Install ninja via VS2019 installer.
  Download and install clang on Windows from <a class="underlined"
  href="https://releases.llvm.org/download.html">llvm.org</a>. Below
  is an example of commands for building SLEEF with Clang on Windows.
</p>

<pre class="command">D:\sleef-3.X> "c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
D:\sleef-3.X> mkdir build
D:\sleef-3.X> cd build
D:\sleef-3.X\build> cmake -GNinja -DCMAKE_C_COMPILER:PATH="C:\Program Files\LLVM\bin\clang.exe" ..
D:\sleef-3.X\build> ninja</pre>


<h2 id="hello">Compiling and running "Hello SLEEF"</h2>

<p class="noindent">
  Now, let's try compiling the <a class="underlined"
  href="hellox86.c">source code shown in Fig. 2.1</a>.
</p>

<pre class="code">
<code>#include &lt;stdio.h&gt;</code>
<code>#include &lt;x86intrin.h&gt;</code>
<code>#include &lt;sleef.h&gt;</code>
<code></code>
<code>int main(int argc, char **argv) {</code>
<code>  double a[] = {2, 10};</code>
<code>  double b[] = {3, 20};</code>
<code></code>
<code>  __m128d va, vb, vc;</code>
<code>  </code>
<code>  va = _mm_loadu_pd(a);</code>
<code>  vb = _mm_loadu_pd(b);</code>
<code></code>
<code>  vc = Sleef_powd2_u10(va, vb);</code>
<code></code>
<code>  double c[2];</code>
<code></code>
<code>  _mm_storeu_pd(c, vc);</code>
<code></code>
<code>  printf("pow(%g, %g) = %g\n", a[0], b[0], c[0]);</code>
<code>  printf("pow(%g, %g) = %g\n", a[1], b[1], c[1]);</code>
<code>}</code>
</pre>
<p style="text-align:center;">
  Fig. 2.1: <a href="hellox86.c">Source code for testing</a>
</p>



<p style="margin-top: 2cm;">
  Fig.2.2 shows typical commands for compiling and executing the hello
  code on Linux computers.
</p>

<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ gcc hellox86.c -o hellox86 -lsleef
$ ./hellox86
pow(2, 3) = 8
pow(10, 20) = 1e+20
$ &block;</pre>
<p style="text-align:center;">
  Fig. 2.2: Commands for compiling and executing hellox86.c
</p>

<p style="margin-top: 2cm;">
  You may need to set LD_LIBRARY_PATH environment variable
  appropriately. If you are trying to execute the program on Mac OSX
  or Windows, try copying the DLLs to the current directory.
</p>

<h2 id="import">Importing SLEEF into your project</h2>


<p class="noindent">
  Below is an example <a class="underlined"
  href="CMakeLists.txt">CMakeLists.txt</a> for compiling the above
  hellox86.c. CMake will automatically download SLEEF from GitHub
  repository, and thus there is no need to include SLEEF in your
  software package. If you prefer importing SLEEF as a submodule in
  git, you can use SOURCE_DIR option instead of GIT_REPOSITORY option
  for ExternalProject_Add.
</p>

<pre class="code">
<code>cmake_minimum_required(VERSION 3.5.1)</code>
<code>include(ExternalProject)</code>
<code>find_package(Git REQUIRED)</code>
<code></code>
<code>ExternalProject_Add(libsleef</code>
<code>  GIT_REPOSITORY https://github.com/shibatch/sleef</code>
<code>  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/contrib</code>
<code>)</code>
<code></code>
<code>include_directories(${CMAKE_BINARY_DIR}/contrib/include)</code>
<code>link_directories(${CMAKE_BINARY_DIR}/contrib/lib)</code>
<code></code>
<code>add_executable(hellox86 hellox86.c)</code>
<code>add_dependencies(hellox86 libsleef)</code>
<code>target_link_libraries(hellox86 sleef)</code>
</pre>
<p style="text-align:center;">
  Fig. 2.3: <a href="CMakeLists.txt">Example CMakeLists.txt</a>
</p>

<p class="footer">
  Copyright &copy; <!--YEAR--> SLEEF Project.<br/>
  SLEEF is open-source software and is distributed under the Boost Software License, Version 1.0.
</p>

<!--TEST-->

</body>
</html>
